<template>
  <div class="container">
    <Paglist
      v-model="dataList"
      :is-init-api="true"
      :parame="parame"
      @register="register"
      :scroll-top-bottom="260"
      :is-show-loadmore="dataList.length >= 1"
    >
      <template v-for="v in dataList" :key="v.id">
        <view class="list-item">
          <BusinessOpportunitiesItem
            v-bind="v"
            :isMyManage="false"
            @remove="handleRemove(v)"
            @detail="handleDetail(v)"
            @follow="handleFollow(v)"
            @share="handleShare(v)"
            @collect="handleCollect(v)"
            @set-top="handleStop(v)"
            @close="handleClose(v)"
          ></BusinessOpportunitiesItem>
        </view>
      </template>
    </Paglist>
  </div>
</template>
<script setup lang="ts">
  import { boLibList, closeBoSys, removeBo } from '@/api/businessOpportunities';
  import type { IBoLibListQuery, IBusinessOpportunityList } from '@/api/businessOpportunities/type';
  import Paglist from '@/components/Paglist/index.vue';
  import { useConList } from '@/components/Paglist/useConList';
  import { ref, provide, type PropType, watch } from 'vue';
  import BusinessOpportunitiesItem from './BusinessOpportunitiesItem.vue';
  import { Toast } from '@/utils';
  import { navigateTo } from '@/router';
  import { behaviorCollectOperate } from '@/api';
  import { BehaviorCollectTypeEnum } from '@/enums/publicModuleEnum';
  import { setBoTop } from '@/api/businessOpportunities';

  const props = defineProps({
    parame: {
      type: Object as PropType<IBoLibListQuery>,
      default: () => ({
        industryCategoryIdList: [],
        assType: void 0,
        keyWord: '',
        current: 1,
        size: 10,
      }),
    },
  });
  const emits = defineEmits(['set', 'share']);
  provide('api', async (obj: IBoLibListQuery) => {
    const res = await boLibList(obj);
    return res;
  });
  const dataList = ref<IBusinessOpportunityList[]>([]);

  const [register, { refresh }] = useConList();

  const handleShare = (v: IBusinessOpportunityList) => emits('share', v);

  const handleRemove = (v: IBusinessOpportunityList) => {
    removeBo(v.id).then(() => {
      refresh();
      Toast('已移出本会');
    });
  };
  const handleClose = async (v: IBusinessOpportunityList) => {
    await closeBoSys(v.id);
    refresh();
    Toast('已关闭');
  };
  const handleFollow = (v: IBusinessOpportunityList) => {
    navigateTo({
      name: 'followUpRecords',
      options: {
        boInfoId: v.id,
      },
    });
  };

  const handleDetail = (v: IBusinessOpportunityList) => {
    navigateTo({
      name: 'supplyDemandDetails',
      options: {
        id: v.id,
        successFn: () => {
          refresh();
        },
      },
    });
  };

  const handleCollect = (v: IBusinessOpportunityList) => {
    behaviorCollectOperate({ type: BehaviorCollectTypeEnum.BO, dataId: v.id }).then((bol) => {
      if (bol) {
        uni.showModal({
          title: '收藏提示',
          content:
            '您已收藏供需信息成功！可前往【我的】-【我的供需】-【我收藏的】查看已收藏的供需列表。',
          confirmText: '我知道了',
          showCancel: false,
        });
      }
      v.collect = bol;
    });
  };

  function handleStop(v: IBusinessOpportunityList) {
    setBoTop({ boMapId: v.boMapId, top: !v.top }).then(() => {
      Toast('操作成功');
      refresh();
    });
  }

  watch(
    () => props.parame,
    () => {
      refresh();
    },
    { deep: true, immediate: true },
  );

  defineExpose({
    refresh,
  });
</script>
<style lang="scss" scoped>
  .list-item {
    padding: 0 30rpx;
    margin-bottom: 30rpx;
    &:nth-child(1) {
      padding-top: 30rpx;
    }
  }
</style>
